package com.music.web.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.music.biz.AlbumBiz;
import com.music.biz.GenreBiz;
import com.music.entity.Album;
import com.music.entity.Genre;

@Controller
public class indexController {

	@Autowired
	private GenreBiz genreBiz;
	@Autowired
	private AlbumBiz albumBiz;


	@RequestMapping({ "/index", "/" })
	public String index(Model model,HttpSession session) {
		List<Album> albums = albumBiz.getHotAlbums(9);
		List<Genre> genres = genreBiz.getAll();
		model.addAttribute("albums", albums);
		session.setAttribute("genres", genres);
		return "index";
	}

	
	@RequestMapping("/details")
	public String details(int id, Model model) {
		Album album = albumBiz.fetchById(id);
		model.addAttribute("album", album);
		return "album";
	}

	@RequestMapping("/select")
	public String selectAlbum(
			@RequestParam(defaultValue = "0", required = false) int genreId,
			String title,
			@RequestParam(defaultValue = "1", required = false) int page,
			Model model) {
		int pageSize = 6;
		int start = (page - 1) * pageSize;
		// 查询当前页的记录
		List<Album> albums = albumBiz.getAlbumsByConditions(0, genreId, title,
				start, pageSize);
		model.addAttribute("albums", albums);
		// 总记录数
		int rows = albumBiz.getRowCountByConditions(0, genreId, title);
		// 总页数
		int totalPage = (int) Math.ceil((double) rows / pageSize);
		model.addAttribute("totalPages", totalPage);
		model.addAttribute("title", title);
		model.addAttribute("genreId", genreId);
		model.addAttribute("page", page);
		return "album_list";
	}

}
